package com.spic.business.riskbase.hierarchy.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.spic.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.spic.business.riskbase.hierarchy.domain.RiskDangerSource;
import com.spic.business.riskbase.hierarchy.mapper.RiskWorkStepMapper;
import com.spic.business.riskbase.hierarchy.domain.RiskWorkStep;
import com.spic.business.riskbase.hierarchy.service.IRiskWorkStepService;

/**
 * work_stepService业务层处理
 *
 * @author ruoyi
 * @date 2023-04-14
 */
@Service
public class RiskWorkStepServiceImpl implements IRiskWorkStepService
{
    @Autowired
    private RiskWorkStepMapper riskWorkStepMapper;

    /**
     * 查询work_step
     *
     * @param id work_step主键
     * @return work_step
     */
    @Override
    public RiskWorkStep selectRiskWorkStepById(Long id)
    {
        return riskWorkStepMapper.selectRiskWorkStepById(id);
    }

    /**
     * 查询work_step列表
     *
     * @param riskWorkStep work_step
     * @return work_step
     */
    @Override
    public List<RiskWorkStep> selectRiskWorkStepList(RiskWorkStep riskWorkStep)
    {
        return riskWorkStepMapper.selectRiskWorkStepList(riskWorkStep);
    }

    /**
     * 新增work_step
     *
     * @param riskWorkStep work_step
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRiskWorkStep(RiskWorkStep riskWorkStep)
    {
        int rows = riskWorkStepMapper.insertRiskWorkStep(riskWorkStep);
        insertRiskDangerSource(riskWorkStep);
        return rows;
    }

    /**
     * 修改work_step
     *
     * @param riskWorkStep work_step
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRiskWorkStep(RiskWorkStep riskWorkStep)
    {
        riskWorkStepMapper.deleteRiskDangerSourceByWorkStepId(riskWorkStep.getId());
        insertRiskDangerSource(riskWorkStep);
        return riskWorkStepMapper.updateRiskWorkStep(riskWorkStep);
    }

    /**
     * 批量删除work_step
     *
     * @param ids 需要删除的work_step主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRiskWorkStepByIds(Long[] ids)
    {
        riskWorkStepMapper.deleteRiskDangerSourceByWorkStepIds(ids);
        return riskWorkStepMapper.deleteRiskWorkStepByIds(ids);
    }

    /**
     * 删除work_step信息
     *
     * @param id work_step主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRiskWorkStepById(Long id)
    {
        riskWorkStepMapper.deleteRiskDangerSourceByWorkStepId(id);
        return riskWorkStepMapper.deleteRiskWorkStepById(id);
    }

    @Override
    public int batchRiskWorkStep(List<RiskWorkStep> list) {
        return riskWorkStepMapper.batchRiskWorkStep(list);
    }

    @Override
    public int deleteRiskWorkStepByWorkTaskId(Long id) {
        return riskWorkStepMapper.deleteRiskWorkStepByWorkTaskId(id);
    }

    /**
     * 新增${subTable.functionName}信息
     *
     * @param riskWorkStep work_step对象
     */
    public void insertRiskDangerSource(RiskWorkStep riskWorkStep)
    {
        List<RiskDangerSource> riskDangerSourceList = riskWorkStep.getRiskDangerSourceList();
        Long id = riskWorkStep.getId();
        if (StringUtils.isNotNull(riskDangerSourceList))
        {
            List<RiskDangerSource> list = new ArrayList<RiskDangerSource>();
            for (RiskDangerSource riskDangerSource : riskDangerSourceList)
            {
                riskDangerSource.setWorkStepId(id);
                list.add(riskDangerSource);
            }
            if (list.size() > 0)
            {
                riskWorkStepMapper.batchRiskDangerSource(list);
            }
        }
    }
}
